<include file="header" />
<body>
<div class="wraper">
    <div class="top">
        <a href="javascript:history.go(-1);" class="iconfont icon-zuo pull-left"></a>
        <span class="pull-left">电量查询</span>
        <a href="{BEESCRM::U('base/dormCharge/index',array('wechat_id'=>$wechat_id))}" class="iconfont icon-shouye pull-right text-right"></a>
    </div>
    <div class="margin-top">
        <p class="form-box">
            <select id="community_name">
                <option value="0">请选择所在生活区</option>
	            <volist name="community" id="vo">
		            <option value="{BEESCRM:$vo.community_id}" >{BEESCRM:$vo.community_name}</option>
	            </volist>
            </select>
            <span class="triangle"></span>
        </p>
        <p class="form-box">
            <select id="building_name">
                <option value="0">请选择楼号</option>
            </select>
            <span class="triangle"></span>
        </p>
        <p class="form-box">
	        <select id="dorm">
		        <option value="0">请选择寝室</option>
	        </select>
	        <span class="triangle"></span>
        </p>
    </div>
    <div class="btn-box">
        <a href="javascript:void(0);" class="btn">立即查询</a>
    </div>
</div>
<include file="commonJS" />
<script>
    $('.tab-prize').on('click','.option',function(){
        if($(this).hasClass('other')){
            $(this).siblings().removeClass('checked');
        }
        else{
            $(this).toggleClass('checked').siblings().removeClass('checked');
        }
    })

    $("#community_name").change(function () {
	    var id = $("#community_name").val();
	    var build = getParams(id);
	    var options = [];
	    if(Number(build) == 0){
		    var option = '<option value="0" data-num="0">请选择楼号</option>';
		    options.push(option);
	    }else{
		    var option = '<option value="0" data-num="0">请选择楼号</option>';
		    options.push(option);
		    $.each(build, function (index, node) {
			    var option = '<option value="' + node.building_id+'">' + node.building_name + '</option>';
			    options.push(option);
		    });
	    }
	    $("#building_name").html(options.join(''));
    });

    $("#building_name").change(function () {
	    var id = $("#building_name").val();
	    var dorm = getDorm(id);
	    var options = [];
	    if(Number(dorm) == 0){
		    var option = '<option value="0">请选择寝室</option>';
		    options.push(option);
	    }else{
		    var option = '<option value="0">请选择寝室</option>';
		    options.push(option);
		    $.each(dorm, function (index, node) {
			    var option = '<option value="' + node.dorm_id+'">' + node.dorm_name + '</option>';
			    options.push(option);
		    });
	    }
	    $("#dorm").html(options.join(''));
    });

    function getParams(id) {
	    id = id ? id : 0;
	    var result = null;
	    $.ajax({
		    url: "/base/dormCharge/getBuilding/wechat_id/{BEESCRM:$wechat_id}",
		    type: "post",
		    dataType: 'json',
		    async: false,
		    data: {
			    id: id
		    },
		    success: function (data) {
			    if (data.status == 'S') {
				    result = data.data;
			    }
		    }
	    });
	    return result;
    }

    function getDorm(id) {
	    id = id ? id : 0;
	    var result = null;
	    $.ajax({
		    url: "/base/dormCharge/getDormInfo/wechat_id/{BEESCRM:$wechat_id}",
		    type: "post",
		    dataType: 'json',
		    async: false,
		    data: {
			    id: id
		    },
		    success: function (data) {
			    if (data.status == 'S') {
				    result = data.data;
			    }
		    }
	    });
	    return result;
    }

	$('.btn').on('click',function(){
		var dorm = $('#dorm').val();

		if(dorm == 0 || !dorm){
			$.alert('error','请选择寝室');
			return false;
		}

		var str = "/dorm_id/"+dorm+"/flag/1";
		window.location.href="/base/dormCharge/queryResult/wechat_id/{BEESCRM:$wechat_id}"+str;
    });
</script>
</body>
</html>